local AbsActExcel = require("excel.absAct")
local Lang = require("common.Lang")
local Util = require("common.Util")
local Msg = require("core.Msg")
local Broadcast = require("broadcast.Broadcast")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local ItemDefine = require("bag.ItemDefine")
local AbsActDefine = require("absAct.AbsActDefine")
local AbsActLogic = require("absAct.AbsActLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local HeroGrid = require("hero.HeroGrid")
local PanelDefine = require("broadcast.PanelDefine")
local HeroDefine = require("hero.HeroDefine")
--[[
   absAct.xlxs-everyDayPray
   新英雄来袭-每日祈福
   DB:
        human.db.absAct[id] = {
            itemGet = xxx,         -- 最后的奖励是否已领取 1已领 0 未领
            xxx = 1,               -- 指定天数奖励是否领取了
            ...
        }
   public:
        getAndSendMsg()             -- 发送活动数据
        isRed()                     -- 红点提醒
        isActive()                  -- 激活状态
        isOpen()                    -- 活动开启
        get()                       -- 领取奖励
--]]

function getAndSendMsg(human, id)
    local absConfig = AbsActExcel.absActivity[id]
    local config = AbsActExcel.everyDayPray[absConfig.actId]
    if not config then return end

    AbsActLogic.checkAbsActClean(human, id)
    local absAct = human.db.absAct[id]
    
    local msgRet = Msg.gc.GC_ABS_ACT_EVERY_DAY_PRAY_QUERY
    msgRet.openDay = Util.diffDay(absConfig.realStartTime) + 1
    local heroCnt = 0
    for k, v in ipairs(config.items) do
        Grid.makeItem(msgRet.items[k], v[1], v[2])
        if HeroDefine.isHero(v[1]) then        
            heroCnt = heroCnt + 1    
            HeroGrid.makeHeroSimpleByID(msgRet.heroSimple[heroCnt], v[1])
        end
    end
    msgRet.itemGet = absAct.itemGet and 1 or 0
    msgRet.get[0] = 0
    for k, v in ipairs(config.loginReward) do
        Grid.makeItem(msgRet.login[k], v[1], v[2])
        if absAct[k] then
            msgRet.get[0] = msgRet.get[0] + 1 
            msgRet.get[msgRet.get[0]] = k
        end
    end
    msgRet.heroSimple[0] = heroCnt
    msgRet.items[0] = #config.items
    msgRet.login[0] = #config.loginReward
    Msg.send(msgRet,human.fd)
end

function isRed(human, YYInfo, funcConfig)
    local state = AbsActLogic.isStarted(human, funcConfig.funcID)
    if not state then return end

    AbsActLogic.checkAbsActClean(human, funcConfig.funcID)

    local absAct = human.db.absAct[funcConfig.funcID]
    if not absAct then return end

    local absConfig = AbsActExcel.absActivity[funcConfig.funcID]
    local config = AbsActExcel.everyDayPray[absConfig.actId]
    
    local day = Util.diffDay(absConfig.realStartTime) + 1
    day = day > #config.loginReward and #config.loginReward or day
    for i = 1, day do
        if not absAct[i] then
            return true
        end
    end

    if day >= #config.loginReward then
        if not absAct.itemGet then 
            return true
        end
    end
end

function isActive(human, YYInfo, funcConfig)
    return not isOpen(human, YYInfo, funcConfig)
end

function isOpen(human, YYInfo, funcConfig)
    return AbsActLogic.isStarted(human, funcConfig.funcID)
end

function get(human, type, id, param)
    local state= AbsActLogic.isStarted(human,  id)
    if not state then return end

    AbsActLogic.checkAbsActClean(human, id)
    local absAct =  human.db.absAct[id]
    local absConfig = AbsActExcel.absActivity[id]
    local config = AbsActExcel.everyDayPray[absConfig.actId]
    if not config then return end

    local day = Util.diffDay(absConfig.realStartTime) + 1
    if type == 0 then
        day = day > #config.loginReward and #config.loginReward or day
        
        BagLogic.cleanMomentItemList()
        local len = 0
        for i = 1, day do
           if not absAct[i] then
              absAct[i] = 1
              len = len + 1
              BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, config.loginReward[i][1], config.loginReward[i][2])
           end
        end

        if len > 0 then
           BagLogic.addMomentItemList(human, "abs_every_day_pray_get")
        end
    else
        if day < #config.loginReward then
            return 
        end
        if absAct.itemGet then return end

        absAct.itemGet = 1
        BagLogic.cleanMomentItemList()
        BagLogic.updateMomentItem(BagLogic.ADDITEM_TYPE_1, config.items[param][1], config.items[param][2])
        BagLogic.addMomentItemList(human, "abs_every_day_pray_get")
    end

    getAndSendMsg(human, id)

    YunYingLogic.sendBanner(human)
    YunYingLogic.updateIcon(YYInfo[id], human)
    YunYingLogic.sendGroupUpdate(YYInfo[id], human, PanelDefine.PANEL_ID_5011)
end